﻿using SD.Infrastructure.EntityBase;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Text;

namespace SlamDunk.MES.Domain.Entities.StockContext
{
    /// <summary>
    /// 物料批次
    /// </summary>
    public class MaterialLot : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected MaterialLot() { }
        #endregion

        #region 01.创建物料批次构造器
        /// <summary>
        /// 创建物料批次构造器
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialNo">物料定义编号</param>
        /// <param name="materialName">物料定义名称</param>
        /// <param name="supplierId">供应商Id</param>
        /// <param name="supplierNo">供应商编号</param>
        /// <param name="supplierName">供应商名称</param>
        /// <param name="productionBatchNo">生产批号</param>
        /// <param name="producedDate">生产日期</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="expiredDate">过期日期</param>
        /// <param name="receptionDate">接收日期</param>
        /// <param name="specification">规格</param>
        /// <param name="initialQuantity">初始数量</param>
        /// <param name="materialSourceType">物料来源</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public MaterialLot(Guid materialId, string materialNo, string materialName, Guid supplierId, string supplierNo, string supplierName, string productionBatchNo, DateTime producedDate, TimeSpan guaranteePeriod, DateTime expiredDate, DateTime receptionDate, string specification, decimal? initialQuantity, MaterialSourceType materialSourceType, Guid? facilityId, string description)
            : this()
        {
            this.MaterialId = materialId;
            this.MaterialNo = materialNo;
            this.MaterialName = materialName;
            this.SupplierId = supplierId;
            this.SupplierNo = supplierNo;
            this.SupplierName = supplierName;
            this.ProductionBatchNo = productionBatchNo;
            this.ProducedDate = producedDate.Date;
            this.GuaranteePeriodTicks = guaranteePeriod.Ticks;
            this.ExpiredDate = expiredDate.Date;
            this.ReceptionDate = receptionDate.Date;
            this.Specification = specification;
            this.InitialQuantity = initialQuantity;
            this.MaterialSourceType = materialSourceType;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 物料定义Id —— Guid MaterialId
        /// <summary>
        /// 物料定义Id
        /// </summary>
        public Guid MaterialId { get; private set; }
        #endregion

        #region 物料定义编号 —— string MaterialNo
        /// <summary>
        /// 物料定义编号
        /// </summary>
        public string MaterialNo { get; private set; }
        #endregion

        #region 物料定义名称 —— string MaterialName
        /// <summary>
        /// 物料定义名称
        /// </summary>
        public string MaterialName { get; private set; }
        #endregion

        #region 供应商Id —— Guid SupplierId
        /// <summary>
        /// 供应商Id
        /// </summary>
        public Guid SupplierId { get; private set; }
        #endregion

        #region 供应商编号 —— string SupplierNo
        /// <summary>
        /// 供应商编号
        /// </summary>
        public string SupplierNo { get; private set; }
        #endregion

        #region 供应商名称 —— string SupplierName
        /// <summary>
        /// 供应商名称
        /// </summary>
        public string SupplierName { get; private set; }
        #endregion

        #region 生产批号 —— string ProductionBatchNo
        /// <summary>
        /// 生产批号
        /// </summary>
        public string ProductionBatchNo { get; private set; }
        #endregion

        #region 生产日期 —— DateTime ProducedDate
        /// <summary>
        /// 生产日期
        /// </summary>
        public DateTime ProducedDate { get; private set; }
        #endregion

        #region 保质期 —— TimeSpan GuaranteePeriod
        /// <summary>
        /// 保质期
        /// </summary>
        public long GuaranteePeriodTicks { get; private set; }

        /// <summary>
        /// 保质期
        /// </summary>
        public TimeSpan GuaranteePeriod
        {
            get => TimeSpan.FromTicks(this.GuaranteePeriodTicks);
        }
        #endregion

        #region 过期日期 —— DateTime ExpiredDate
        /// <summary>
        /// 过期日期
        /// </summary>
        public DateTime ExpiredDate { get; private set; }
        #endregion

        #region 接收日期 —— DateTime ReceptionDate
        /// <summary>
        /// 接收日期
        /// </summary>
        public DateTime ReceptionDate { get; private set; }
        #endregion

        #region 规格 —— string Specification
        /// <summary>
        /// 规格
        /// </summary>
        public string Specification { get; private set; }
        #endregion

        #region 初始数量 —— decimal? InitialQuantity
        /// <summary>
        /// 初始数量
        /// </summary>
        public decimal? InitialQuantity { get; private set; }
        #endregion

        #region 物料来源 —— MaterialSourceType MaterialSourceType
        /// <summary>
        /// 物料来源
        /// </summary>
        public MaterialSourceType MaterialSourceType { get; private set; }
        #endregion

        #region 工厂模型Id —— Guid? FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        public Guid? FacilityId { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改物料批次 —— void UpdateInfo(Guid supplierId...
        /// <summary>
        /// 修改物料批次
        /// </summary>
        /// <param name="supplierId">供应商Id</param>
        /// <param name="supplierNo">供应商编号</param>
        /// <param name="supplierName">供应商名称</param>
        /// <param name="productionBatchNo">生产批号</param>
        /// <param name="producedDate">生产日期</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="expiredDate">过期日期</param>
        /// <param name="receptionDate">接收日期</param>
        /// <param name="specification">规格</param>
        /// <param name="initialQuantity">初始数量</param>
        /// <param name="materialSourceType">物料来源</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(Guid supplierId, string supplierNo, string supplierName, string productionBatchNo, DateTime producedDate, TimeSpan guaranteePeriod, DateTime expiredDate, DateTime receptionDate, string specification, decimal? initialQuantity, MaterialSourceType materialSourceType, Guid? facilityId, string description)
        {
            this.SupplierId = supplierId;
            this.SupplierNo = supplierNo;
            this.SupplierName = supplierName;
            this.ProductionBatchNo = productionBatchNo;
            this.ProducedDate = producedDate.Date;
            this.GuaranteePeriodTicks = guaranteePeriod.Ticks;
            this.ExpiredDate = expiredDate.Date;
            this.ReceptionDate = receptionDate.Date;
            this.Specification = specification;
            this.InitialQuantity = initialQuantity;
            this.MaterialSourceType = materialSourceType;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.SupplierName);
            builder.Append(this.ProductionBatchNo);
            builder.Append(this.Specification);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
